# -*- coding: utf-8 -*-
import json
from rest_framework import serializers
from workorder.models import FlowConf,ApproveConf,WorkOrder,SubOrder
from user.models import User, Role


class FlowConfSerializer(serializers.ModelSerializer):
    class Meta:
        model = FlowConf
        fields = '__all__'


class ApproveConfSerializer(serializers.ModelSerializer):
    approve_user = serializers.SerializerMethodField(required=False)
    flowconf = serializers.CharField(source='flowconf.name',required=False)
    flowconf_id = serializers.CharField(required=True)

    class Meta:
        model = ApproveConf
        fields = '__all__'

    def get_approve_user(self,row):
        approve_type_id = row.approve_type_id  # 审批类型ID
        approve_id = row.approve_id            # 角色ID、用户ID
        if approve_type_id == '1':
            '''用户审批'''
            approve = User.objects.get(id=approve_id)
            return {
                'approve': approve.username,
                'approve_type':'指定人员'
            }
        elif approve_type_id == '2':
            '''角色审批'''
            approve = Role.objects.get(id=approve_id)
            return {
                'approve': approve.name,
                'approve_type': '角色组审批'
            }
        else:
            '''其他审批类型待补充'''

    def create(self, validated_data):
        # {'flowconf': {'name': '请假工单'}, 'flowconf_id': '1', 'sequence': 1, 'approve_type_id': '1', 'approve_id': '1'}
        approve_conf = ApproveConf.objects.create(
            flowconf_id=validated_data.get('flowconf_id'),
            approve_type_id=validated_data.get('approve_type_id'),
            sequence=validated_data.get('sequence'),
            approve_id=validated_data.get('approve_id'),

        )
        return approve_conf


class FlowConfDeepSerializer(FlowConfSerializer):
    approveconf_set = ApproveConfSerializer(many=True,required=False)


class WorkOrderSerializer(serializers.ModelSerializer):
    create_user=serializers.CharField(source='create_user.username')
    flowconf=serializers.CharField(source='flowconf.name')
    order_status_name=serializers.SerializerMethodField(required=False)
    parameter=serializers.SerializerMethodField()

    class Meta:
        model = WorkOrder
        fields = '__all__'

    def get_order_status_name(self,row):
        status_cat = dict(row.status_cat)
        return status_cat.get(row.order_status)

    def get_parameter(self,row):
        return json.loads(row.parameter)


class SubOrderSerializer(serializers.ModelSerializer):
    create_user=serializers.CharField(source='mainorder.create_user')
    workorder_id = serializers.CharField(source='mainorder.id')
    action_status_name = serializers.SerializerMethodField(required=False)
    suborder_status_name = serializers.SerializerMethodField(required=False)
    mainorder = serializers.CharField(source='mainorder.flowconf.name')
    approve_user = serializers.SerializerMethodField(required=False)

    class Meta:
        model = SubOrder
        fields = '__all__'

    def get_action_status_name(self,row):
        # action_cat = dict(row.action_cat)
        # return action_cat.get(row.action_status)
        return row.get_action_status_display()

    def get_suborder_status_name(self,row):
        action_cat = dict(row.suborder_cat)
        return action_cat.get(row.suborder_status)

    def get_approve_user(self,row):
        if row.approve_user:
            return row.approve_user.username


class WorkOrderDeepSerializer(WorkOrderSerializer):
    suborder_set = SubOrderSerializer(many=True, required=False)


